import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/components/button_components/base_icon_button.dart';

/// 用于密码密文输入框右边的是否显示按钮
class BaseDisplayButton extends StatefulWidget {
  final bool isDisplay;
  final Function({@required bool isDisplay}) onClick;

  const BaseDisplayButton({Key key, this.isDisplay = false, @required this.onClick}) : super(key: key);

  @override
  State<BaseDisplayButton> createState() => _BaseDisplayButtonState();
}

class _BaseDisplayButtonState extends State<BaseDisplayButton> {
  IconData iconData = BaseIconData.x3HidePassword;

  @override
  void initState() {
    super.initState();
    if (widget.isDisplay) iconData = BaseIconData.x3DisplayPassword;
  }

  @override
  Widget build(BuildContext context) {
    return BaseIconButton(background: Colors.transparent, iconSize: 18, icon: iconData, onPress: toggleState);
  }

  /// 切换状态
  void toggleState() {
    if (iconData == BaseIconData.x3DisplayPassword) {
      iconData = BaseIconData.x3HidePassword;
      widget.onClick(isDisplay: false);
      setState(() {});
      return;
    }
    if (iconData == BaseIconData.x3HidePassword) {
      iconData = BaseIconData.x3DisplayPassword;
      widget.onClick(isDisplay: true);
      setState(() {});
      return;
    }
  }
}
